#!/usr/bin/perl

######################################################################
package Swignition::uF::RelLicense;
######################################################################

use Swignition::GenericParser::Utils;
use Swignition::uF;
use XML::LibXML qw(:all);
use strict;


sub consume
{
	my $page   = shift;

	unless ( $page->{Profiles}->{'http://purl.org/uF/rel-license/1.0/'}
	||       $page->{Profiles}->{'http://purl.org/uF/2008/03/'}
	||      !$page->{options}->{uf_strict_profiles})
	{
		return;
	}
	
	$page->{uF}->{License} = [ parse_all($page) ];
	foreach my $license (@{ $page->{uF}->{License} })
	{
		$page->mergeSubject( $license->rdf_subject );
		my $val = Swignition::RDFModel::Value->new($license->uri, 1);
		my $url = $page->uri;
		$page->{RDF}->{$url}->addVal('http://www.w3.org/1999/xhtml/vocab#license', $val);
	}
	if (defined $page->{uF}->{License}->[0])
		{ $page->mergeNS( $page->{uF}->{License}->[0] ); }
}


sub parse_all
{
	my $page   = shift;
	my $within = shift || $page->{DOM};
	my @rv;
	
	my @nodes = searchRel('license', $within);
	
	foreach my $l (@nodes)
	{
		my $l_parsed = parse($page, $l);
		push @rv, $l_parsed;
	}
	
	return @rv;
} #/sub parse_all


sub uri
{
	my $this = shift;
	my $all  = shift;
	my @rv;
	
	if ($this->{license})
	{
		push @rv, $this->{license};
	}
	if (length $this->{_id})
	{
		push @rv, $this->{_page}->uri.'#'.$this->{_id};
	}
	push @rv, Swignition::GenericParser::Utils::BNodeURI($this->{_dom}, 'License')
		unless (@rv);
	
	return $rv[0] unless (defined $all);
	while ($all) { shift @rv; $all--; }
	return @rv;
} #/sub uri


sub metadata_ns
{
	my $this = shift;
	return { 'cc' => { nsurl=>'http://creativecommons.org/ns#',title=>'Creative Commons' } };
} #/sub namespace


sub rdf_subject
{
	my $this    = shift;

	# Create RDF subject
	my $subject = Swignition::RDFModel::Subject->new($this->uri);
	$subject->addObject($this);
	$subject->setDom($this->{_dom});
	
	$subject->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
		Swignition::RDFModel::Value->new('http://creativecommons.org/ns#License', 1));

	if (length $this->{title})
	{
		my $val = Swignition::RDFModel::Value->new($this->{title});
		$subject->addVal('http://purl.org/dc/terms/title', $val);
	}
	
	return $subject;
	
} #/sub rdf_subject


sub parse
{
	my $page = shift;
	my $rv = { '_dom'=>shift };
	my $pkg = __PACKAGE__;
	if (defined $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') })
		{ return $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') }; }
	else
		{ $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') } = $rv; }
	
	my $t_node = $rv->{'_dom'}->cloneNode(1);

	my $id = $t_node->getAttribute('id');
	$rv->{_id} = $id if (length $id);
	$rv->{_page} = $page;
	
	$rv->{'license'} = $page->uri($t_node->getAttribute('href'));
	if (length $t_node->getAttribute('title'))
		{ $rv->{'title'} = $t_node->getAttribute('title'); }
	else
		{ $rv->{'title'} = STRINGIFY($t_node, 'value'); }

	bless $rv;
	return $rv;	
} #/sub parse


1; #/package Swignition::uF::RelLicense




